package org.yx.service.smat.hold_material_sn;

import java.util.ArrayList;
import java.util.List;

import org.yx.exception.BizException;
import org.yx.models.SmatMsnSts;
import org.yx.models.SqcmHldDef;
import org.yx.service.bas.create_rule_code.BasCreateRuleCodeService;
import org.yx.service.smat.insert_material_sn_history.SmatInsertMaterialSnHistoryIn;
import org.yx.service.smat.insert_material_sn_history.SmatInsertMaterialSnHistoryService;
import org.yx.service.smat.view_material_sn.MatMsn;
import org.yx.service.smat.view_material_sn.SmatViewMaterialSnIn;
import org.yx.service.smat.view_material_sn.SmatViewMaterialSnService;
import org.yx.utils.BaseService;
import org.yx.utils.FieldMap;
import org.yx.utils.ServiceBeanUtil;

import com.xiaoleilu.hutool.collection.CollUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 锁定物料批号
 * 
 * @author zxw
 * @since 2018/05/19
 **/
public class SmatHoldMaterialSnService extends BaseService {
	private static final String HOLD_CODE_RULE = "QC_HOLD_NO";

	public SmatHoldMaterialSnOut holdMaterialSn(SmatHoldMaterialSnIn in) throws Exception {
		if (StrUtil.isBlank(in.getHoldCode())) {
			throw new BizException("SYS-0004", new FieldMap("parameter", "holdCode"));
		}

		SmatViewMaterialSnIn snIn = new SmatViewMaterialSnIn();
		snIn.setMatCode(in.getMatCode());
		snIn.setMsn(in.getMsn());
		List<MatMsn> msnDataList = ServiceBeanUtil.getBean(SmatViewMaterialSnService.class).viewMaterialSn(snIn)
				.getMsnList();

		if (CollUtil.isEmpty(msnDataList)) {
			throw new BizException("SMAT-0004");// 找不到满足条件的物料批号信息
		}

		List<SmatMsnSts> upsMsnList = new ArrayList<SmatMsnSts>();
		String holdNo = BasCreateRuleCodeService.createRuleCode(HOLD_CODE_RULE);
		for (MatMsn matMsn : msnDataList) {
			if ("D".equals(matMsn.getStatus())) {
				throw new BizException("SMAT-0005", new FieldMap("msn", matMsn.getMsn()));
			}

			if ("H".equals(matMsn.getStatus())) {
				throw new BizException("SMAT-0006", new FieldMap("msn", matMsn.getMsn()), new FieldMap("holdNo",
						matMsn.getHoldNo()));
			}

			SmatMsnSts msnSts = new SmatMsnSts();
			msnSts.setStatus("H");
			msnSts.setEventCode("HOLD");
			msnSts.setEventValue(holdNo);
			msnSts.setHoldNo(holdNo);
			msnSts.setReleaseNo(null);
			msnSts.setOrgCode(matMsn.getOrgCode());
			msnSts.setMsn(matMsn.getMsn());

			upsMsnList.add(msnSts);
		}

		updateBatch(upsMsnList, "status,eventCode,eventValue,holdNo,releaseNo");

		SmatInsertMaterialSnHistoryIn msnHisIn = new SmatInsertMaterialSnHistoryIn();
		msnHisIn.setMsnStsList(upsMsnList);
		ServiceBeanUtil.getBean(SmatInsertMaterialSnHistoryService.class).insertMaterialSnHistory(msnHisIn);

		SqcmHldDef hold = new SqcmHldDef();
		hold.setOrgCode(in.getUserOrgCode());
		hold.setHoldNo(holdNo);
		hold.setHoldType('M');
		hold.setHoldCode(in.getHoldCode());
		hold.setHoldPwd(in.getHoldPwd());
		hold.setHoldUser(in.getUserToken().getUserId());
		hold.setHoldTime(getCurrentTime());
		hold.setMemo(in.getMemo());

		insert(hold);

		SmatHoldMaterialSnOut out = new SmatHoldMaterialSnOut();
		out.setHoldNo(holdNo);

		return out;
	}
}